<template>
  <el-dialog
      v-model="diaVisible"
      :title="title"
      :close-on-click-modal="false"
      :append-to-body="true"
      width="520px"
  >
    <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        status-icon
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username" placeholder="请输入用户ID..."/>
      </el-form-item>
      <el-form-item label="登录密码" prop="passWord">
        <el-input v-model="ruleForm.passWord" placeholder="请输入登录密码..."/>
      </el-form-item>
      <el-form-item label="确认密码" prop="passWord2">
        <el-input v-model="ruleForm.passWord2" placeholder="请再次输入登录密码..."/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <div @click="submitForm" class="sure-btn" style="margin-right: 10px">确认</div>
        <div @click="resetForm" class="cancel-btn">取消</div>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {getPublicKey, editByUserName} from "../../../router/api.js";
import {passwordEncryption} from "../../../util/index.js";

export default {
  name: "forgetUser",
  data() {
    const checkPwd = (rule, value, callback) => {
      if (value !== this.ruleForm.passWord) {
        callback(new Error('密码输入不一致'))
      } else {
        callback()
      }
    }

    return {
      title: '忘记密码',
      diaVisible: false,
      ruleForm: {
        username: '',
        passWord: '',
        passWord2: '',
      },
      ruleFormClone: {},
      rules: {
        username: [
          {required: true, message: '请输用户名', trigger: 'blur'},
        ],
        passWord: [
          {required: true, message: '请输入登录密码', trigger: 'blur'},
        ],
        passWord2: [
          {required: true, validator: checkPwd, trigger: 'change'}
        ],
      },
      currentRow: {}
    }
  },
  created() {
    this.ruleFormClone = JSON.parse(JSON.stringify(this.ruleForm))
  },
  methods: {
    show() {
      this.title = '忘记密码'
      this.diaVisible = true;
    },
    submitForm() {
      this.$refs['ruleFormRef'].validate(async (valid) => {
        if (valid) {
          let {username, passWord} = this.ruleForm;
          let resKey = await getPublicKey({username: encodeURIComponent(username)});
          if (resKey.status != 200) {
            // this.$message.warning('获取公钥失败');
            return;
          }
          let encryptedPassword = passwordEncryption(passWord, resKey.data);
          let params = {
            username,
            password: encryptedPassword
          };
          editByUserName(params).then(res => {
            if (res.data.code == 200) {
              // this.$message.success(res.data.msg);
              this.diaVisible = false;
              this.$emit('editHandle')
            } else {
              // this.$message.warning(res.data.msg)
            }
          })
        }
      });
    },
    resetForm() {
      this.diaVisible = false;
      this.ruleForm = this.ruleFormClone;
      this.$refs['ruleFormRef'].resetFields();
    }
  }
}
</script>

<style scoped>

</style>
